<?php
class IndexAction extends CommonAction{
    public function index(){
		$this->display();
    }
	
	//学生注册
	public function registerForm(){
		$this->getProvince();
		$this->display();
	}
	
	//获取省份列表
	public function getProvince(){
		$province = M('Province');
		$provincelist = $province->query("select Id,Name,ZoneId from __TABLE__");
		$this->assign('province',$provincelist);
	}
	
	//获取学校列表
	public function getSchoolList(){
		$id = $_GET['pro'];
		$highschool = M('Highschool');
		$highschoollist = $highschool->query("select Id,SchoolName from __TABLE__ where ProvinceId={$id}");
		echo json_encode($highschoollist);
	}
	
	//获取专业类别
	public function getMajorCate(){
		$majorcate = M('Majorcate');
		$majorcatelist = $majorcate->query("select Id,Name from __TABLE__");
		$this->assign('majorcate',$majorcatelist);
	}
	//学生注册表单写入
	public function addForm(){
		$users = D('Users');
		$result = $users->create();
		if (!$result){
			// 如果创建失败 表示验证没有通过 输出错误提示信息
			exit($users->getError());
		}else{
			// 验证通过 可以进行其他数据操作
			$username = $this->_post('UserName');
			$orglocation = $this->_post('OrgLocation');
			$schoolid = $this->_post('SchoolId');
			$orgname = $this->_post('OrgName');
			$truename = $this->_post('TrueName');
			$password = md5($this->_post('Password'));
			$phonenumber = $this->_post('PhoneNumber');
			$email = $this->_post('Email');
			$users->execute("insert into __TABLE__ (UserName,OrgLocation,SchoolId,OrgName,TrueName,Password,PhoneNumber,Email,CreateDate,UserType,IsApproved) values ('{$username}','{$orglocation}','{$schoolid}','{$orgname}','{$truename}','{$password}','{$phonenumber}','{$email}',now(),1,0)");
			$loginuser['UserId'] = mysql_insert_id();
			$loginuser['UserName'] = $username;
			$loginuser['TrueName'] = $truename;
			$loginuser['UserType'] = 1;
			$loginuser['OrgName'] = $orgname;
			$_SESSION[C('USER_AUTH_KEY')] = $loginuser;
			$this->success('注册成功', 'stepOne');
		}
	}
	
	//学生注册成功页面
	public function successReg(){
		$this->display();
	}
	
	//学生注册详细页面
	public function detailForm(){
		//判断是否为简单学生
		if($_SESSION[C('USER_AUTH_KEY')]['UserType'] != 1){
			echo '您已经详细注册！';
			exit;
		}
		$userid = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$user = new UsersModel($userid);
		$userdetail = $user->userinfo;
		$this->assign('userdetail',$userdetail);
		//获取省份下拉列表
		$this->getProvince();
		//获取专业类别
		$this->getMajorCate();
		$this->display();
	}
	
	//学生注册详细添加
	public function addDetailForm(){
		$student = D('Student');
		$result = $student->create();
		if (!$result){
			// 如果创建失败 表示验证没有通过 输出错误提示信息
			exit($student->getError());
		}else{
			// 验证通过 可以进行其他数据操作
			$userid = $this->_post('UserId');
			$edulevel = $this->_post('EduLevel');
			$gender = $this->_post('Gender');
			$birthdate = $this->_post('BirthDate').'-'.$this->_post('BirthDate2');
			$enterschooldate = $this->_post('EnterSchoolDate').'-'.$this->_post('GraduateDate2');
			$graduatedate = $this->_post('GraduateDate').'-'.$this->_post('GraduateDate2');;
			$schoolnumber = $this->_post('SchoolNumber');
			$college = $this->_post('College');
			$majorcate = $this->_post('MajorCate');
			$majorname = $this->_post('MajorName');
			$qq = $this->_post('QQ');
			$address = $this->_post('Address1').$this->_post('Address2').$this->_post('Address3').$this->_post('Address4');
			$postalcode = $this->_post('PostalCode');
			$idcard = $this->_post('IdCard');
			$fetion = $this->_post('Fetion');
			$fixedphone = $this->_post('FixedPhone1').'-'.$this->_post('FixedPhone2').'-'.$this->_post('FixedPhone3');
			$weibo = $this->_post('Weibo');
			$personalintroduce = $this->_post('PersonalIntroduce');
			$msn = $this->_post('MSN');
			//更新Users表中的信息
			@M('Users')->execute("update __TABLE__ set OrgLocation = '{$this->_post('OrgLocation')}',SchoolId='{$this->_post('SchoolId')}',OrgName='{$this->_post('OrgName')}',PhoneNumber='{$this->_post('PhoneNumber')}',Email='{$this->_post('Email')}',UserType=2 where UserId='{$this->_post('UserId')}'");

			$student->execute("insert into __TABLE__ (UserId,EduLevel,Gender,BirthDate,EnterSchoolDate,GraduateDate,SchoolNumber,College,MajorCate,MajorName,QQ,Address,PostalCode,IdCard,Fetion,FixedPhone,PersonalIntroduce,MSN,Weibo) values ('{$userid}','{$edulevel}','{$gender}','{$birthdate}','{$enterschooldate}','{$graduatedate}','{$schoolnumber}','{$college}','{$majorcate}','{$majorname}','{$qq}','{$address}','{$postalcode}','{$idcard}','{$fetion}','{$fixedphone}','{$personalintroduce}','{$msn}','{$weibo}')");
			$this->success('完善信息成功', 'successReg');
			$_SESSION[C('USER_AUTH_KEY')]['UserType'] = 2;
			//跳转到个人中心
			echo '<script>window.parent.callbackredirct()</script>';
		}
	}
	
	public function stepThree(){
	
	
	
	}
	
	//用户名唯一验证
	public function checkUsername(){
		$username = $this->_post('UserName');
		$onedata = M('Users')->query("select UserId from __TABLE__ where UserName='{$username}' limit 1");
		echo ($onedata)?(1):(0);		
    }
	//邮箱唯一验证
	public function checkEmail(){
		$email = $this->_post('Email');
		$onedata = M('Users')->query("select UserId from __TABLE__ where Email='{$email}' limit 1");
		echo ($onedata)?(1):(0);		
    }
	//身份证在唯一验证
	public function checkIdCard(){
		$idcard = $this->_post('IdCard');
		$onedata = M('Student')->query("select Id from __TABLE__ where IdCard='{$idcard}' limit 1");
		echo ($onedata)?(1):(0);		
    }
	
    public function Outbox(){
        $this->display();
    }

	//知识竞赛答题
	public function paper(){
		//权限判断
 		if($_SESSION[C('USER_AUTH_KEY')]['UserType']== 1 || $_SESSION[C('USER_AUTH_KEY')]['UserType']== 2){
		
		}else{
			exit('没有权限访问');
		}
		$times = M('Paper')->where("UserId={$_SESSION[C('USER_AUTH_KEY')]['UserId']}")->select();
		$num = count($times);
		if($num > 4){
			exit('每人只能答题五次');
		} 
		//单选(30/1)
		$model = new Model();
		$SingleChoice = $model->query("select Id,Title,Items from questions where Cate=1 and IsApproved=1 order by rand() limit 30");
		foreach($SingleChoice as $k=>$v){
				$itmes[] = explode('%##%',$v['Items']);
		}
		$this->assign('SingleChoice',$SingleChoice);
		$this->assign('itmes',$itmes);
		
		//多选(20/2)
		$model = new Model();
		$MultipleChoice = $model->query("select Id,Title,Items from questions where Cate=2 and IsApproved=1 order by rand() limit 20");
		foreach($MultipleChoice as $k=>$v){
				$Mitmes[] = explode('%##%',$v['Items']);
		}
		$this->assign('MultipleChoice',$MultipleChoice);
		$this->assign('Mitmes',$Mitmes);
		
		//判断(30/1)
		$model = new Model();
		$Judgments = $model->query("select Id,Title,Items from questions where Cate=3 and IsApproved=1 order by rand() limit 30");
		$this->assign('Judgments',$Judgments);
		
		$this->display(Paper);
	}
	public function score(){
		$paper=$this->_post();
		//dump($paper);
		//得出单选题得分
		for($k=1;$k<=30;$k++){
			$SingleChoice[] = $paper['1'.$k];//单选题所做题目ID号组成的数组
			$Sanswer[$k] = $paper['r1'.$k]; 
			$answer = M('questions')->where("Id={$paper['1'.$k]} and Result={$paper['r1'.$k]}")->field('Id')->find();
			if($answer){
				$Sscore+=1;
			}
		}
		$SingleChoice = join(',',$SingleChoice);//把单选题ID号转成字符串存储
		$Sanswer = join(',',$Sanswer);
		
		//得出多选题得分
		for($k=1;$k<=20;$k++){
			$MultipleChoice[] = $paper['2'.$k];
			//$Manswer[] = $paper['r2'.$k];
			$Manswer[$k] = array_sum($paper['r2'.$k]);//把ABCD答案转换成1/2/4/8相加最后组成的结果
			$answer = M('questions')->where("Id={$paper['2'.$k]} and Result={$Manswer[$k]}")->field('Id')->find();
			if($answer){
				$Mscore+=2;
			}
		}
		$MultipleChoice = join(',',$MultipleChoice);//把多选题ID号转成字符串存储
		$Manswer = join(',',$Manswer);
		
		//得出判断题得分
		for($k=1;$k<=30;$k++){
			$TFChoice[$k] = $paper['3'.$k];
			$TFanswer[$k] = $paper['r3'.$k];
			$answer = M('questions')->where("Id={$paper['3'.$k]} and Result={$TFanswer[$k]}")->field('Id')->find();
			if($answer){
				$TFscore+=1;
			}
		}
		$TFChoice = join(',',$TFChoice);//把多选题ID号转成字符串存储
		$TFanswer = join(',',$TFanswer);


		
		$Score = $Sscore+$Mscore+$TFscore;
		$Answer = $Sanswer.','.$Manswer.','.$TFanswer;
		//dump($Answer);
		
		$paper = M('paper');
		$data['UserId'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$data['SingleChoice'] = $SingleChoice;
		$data['MultipleChoice'] = $MultipleChoice;
		$data['TfChoice'] = $TFChoice;
		$data['Score'] = $Score;
		$data['Answer'] = $Answer;
		$data['CreateDate'] = date('Y-m-d H:i:s',time());
		
		
		$paper->add($data);
		$this->assign('Score',$Score);
		$this->display(score);
	}
}
